package com.worktask.mustwakeupalarm;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.format.Time;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
//main activity
public class MustWakeUp extends Activity implements OnClickListener{
	
	private RelativeLayout addBtn;
	
	private RelativeLayout addTime;
	
	private RelativeLayout viewTime;
	
	private TextView showTime;
	
	private String time;
	
	private static int REFRESH_TIME = 1000;
	
	private final static int CHANGE_UI = 1 ;
	
	private static String DATE_FORMAT = "HH:mm";
	
	OurTimerTask mOurTimerTask;
	
	MyUiHandler mUiHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_must_wake_up);
        init();
        
    }
    
    //init all view
    private void init(){
    	
    	addBtn = (RelativeLayout) findViewById(R.id.add_btn);
    	addTime = (RelativeLayout) findViewById(R.id.add_time);
    	viewTime = (RelativeLayout) findViewById(R.id.time_view);
    	showTime = (TextView) findViewById(R.id.time_show);
    	
    	addBtn.setOnClickListener(this);
    	addTime.setOnClickListener(this);
    	viewTime.setOnClickListener(this);
    	showTime.setOnClickListener(this);
    	mOurTimerTask = new OurTimerTask();
    	mUiHandler = new MyUiHandler();
    	updateTime();
    	
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_must_wake_up, menu);
        return true;
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		switch (id) {
		case R.id.add_btn:
			
			break;
			
		case R.id.add_time:
			
			break;
			
		case R.id.time_view:
			
			break;

		default:
			break;
		}
	}
	
	//update time by one second
	private void updateTime(){
		
		Timer mTimer = new Timer();
		mTimer.scheduleAtFixedRate(new OurTimerTask(), new Date(), REFRESH_TIME);
	}
	
	//UI update thread
	class MyUiHandler extends Handler {
		
		public MyUiHandler(){
			
			
		}

		public MyUiHandler (Looper L){
			super(L);
		}
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if(msg.arg1 == 1){
				
				showTime.setText(time);
			}
		}
		
	}
	
	// time thread
	protected class OurTimerTask extends TimerTask{
		
		SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
		@Override
		public void run() {
			// TODO Auto-generated method stub
			time = dateFormat.format(Calendar.getInstance().getTime());
			Message msg = new Message();
			msg.arg1 = CHANGE_UI;
			
			MustWakeUp.this.mUiHandler.sendMessage(msg);
//			showTime.setText(time);
		}
		
	}

    
}
